/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PictureEdit.java
 *
 * Created on 05.09.2010, 13:24:19
 */
package university.kuzenko.ui;

import java.awt.FileDialog;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOError;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author taras
 */
public class PictureEdit extends javax.swing.JDialog {

    File pathToImage;
    BufferedImage image;
    /** Creates new form PictureEdit */
    public PictureEdit(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public void showIMage() throws IOException {
        jLabel1.setText(pathToImage.toString());
        image = ImageIO.read(pathToImage);
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(image, 0, 0, null);
        jLabel1.repaint();
        jButton1.repaint();
    }


    public String modifyPicture(String path) {
        try {
            pathToImage = new File(path);
            if (pathToImage.exists()) {
                showIMage();
            } else {
                FileDialog fd = new FileDialog(this);
                fd.setVisible(true);
                pathToImage = new File(fd.getDirectory(), fd.getFile());
                showIMage();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        setVisible(true);
        return pathToImage.toString();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("jLabel1");

        jButton1.setText("change");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addContainerGap(279, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jLabel1))
                .addContainerGap(266, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       FileDialog fd = new FileDialog(this);
                fd.setVisible(true);
                pathToImage = new File(fd.getDirectory(), fd.getFile());
                try {
                showIMage();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                PictureEdit dialog = new PictureEdit(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
